package us.icitap.timsportal.ws.util;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJBException;

import al.gov.mrp.egjoba.ws.rmis.HandlerClient;
//import al.gov.mrp.egjoba.ws.rmis.Officer;
import us.icitap.timsportal.entity.Officer;
import us.icitap.timsportal.entity.OrgStructure;

public class RmisOfficerThread extends Thread {
	
	private String officerId = null;
	private String name;
	private String surname;
	private Officer officer = null;
	private List<Officer> list = new ArrayList<Officer>();

	public List<Officer> getList() {
		return list;
	}

	public RmisOfficerThread(String officerId) throws SecurityException {
		this.officerId = officerId;
		setDaemon(true);
	}
	
	public RmisOfficerThread(String name, String surname)  throws SecurityException{
		this.name = name;
		this.surname = surname;
		setDaemon(true);
	}
	
	@Override
	public void run() throws EJBException {
		List<al.gov.mrp.egjoba.ws.rmis.Officer> rmisList;
		try {
			if(this.officerId==null){
				
				rmisList = HandlerClient.getSvSoapWebService().allListOfficers(this.name, this.surname);
				if(rmisList!=null){
					for(al.gov.mrp.egjoba.ws.rmis.Officer o:rmisList){
						list.add(castRmisOfficer(o));
					}
				}
			}	
			else {
				System.out.println(this.officerId);
				al.gov.mrp.egjoba.ws.rmis.Officer rmisOfficer = HandlerClient.getSvSoapWebService().allOfficers(officerId);
				if(rmisOfficer != null)
					this.officer = castRmisOfficer(rmisOfficer);
				System.out.println(this.officer);
			}
				
			
			
						
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
			

	}
	
	private Officer castRmisOfficer(al.gov.mrp.egjoba.ws.rmis.Officer o){
		Officer officer = new Officer();
		officer.setOfficerId(o.getEmployeNumber());
		officer.setForename(o.getFirstName());
		officer.setFatherName(o.getSuffix());
		officer.setSurname(o.getLastName());
		if(o.getBirthDate() != null)
			officer.setDob(o.getBirthDate().toGregorianCalendar().getTime());
		officer.setFunction(o.getPositionName());
		officer.setRank(o.getGradeName());
		
//		for (int i = o.getParent().size() - 1; i >= 0; i--) {
//			this.officer.getParentStructureIds().add((int) o.getParent().get(i).getIdParent());
//		}
		
		List<Long> vals = new ArrayList<Long>();
		
		for (int i = o.getParent().size() - 1; i >= 0; i--) {
			vals.add(o.getParent().get(i).getIdParent());
		}
		String val = vals.toString().replaceAll("\\[", "").replaceAll("\\]", "");
		officer.setParentStructureIds(val);
		
		OrgStructure orgStructure = new OrgStructure();
		orgStructure.setId(o.getOrganizationId().intValue());
		orgStructure.setName(o.getOrganizationName());		
		officer.setOrgStructure(orgStructure);
		
		officer.setStructureId(o.getOrganizationId().intValue());
		officer.setStructureName(o.getOrganizationName());

		return officer;
	}

	public Officer getOfficer() {
		return officer;
	}

}
